package com.bgs.engineering.util;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bgs.engineering.api.file.entity.Attachments;
import com.bgs.engineering.api.file.entity.FilesAndModules;
import com.bgs.engineering.api.file.mapper.AttachmentsMapper;
import com.bgs.engineering.api.file.mapper.FilesAndModulesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

/**
 * @author 王宏
 * @version 1.0.0
 * @title FileToOssUtil
 * @description 文件上传到阿里云oss
 * <p></p>
 * @creat 2023/7/23 20:51 Sunday
 **/
@Service
public class FileToOssUtil {
  /**
   * 附件表 mapper
   */
  private static AttachmentsMapper attachmentsMapper;
  /**
   * 附件和模块关联表 mapper
   */
  private static FilesAndModulesMapper filesAndModulesMapper;

  /**
   * 注入 附件表 mapper
   */
  @Autowired
  public void setAttachmentsMapper(AttachmentsMapper attachmentsMapper) {
    FileToOssUtil.attachmentsMapper = attachmentsMapper;
  }

  /**
   * 注入 附件和模块关联表 mapper
   */
  @Autowired
  public void setFilesAndModulesMapper(FilesAndModulesMapper filesAndModulesMapper) {
    FileToOssUtil.filesAndModulesMapper = filesAndModulesMapper;
  }

  /**
   * 文件上传到阿里云oss 并保存到附件表表
   *
   * @param file   文件
   * @param models 模块
   * @return 附件id
   */
  public static Integer fileToOss(MultipartFile file, String models) {
    Integer b = 0;
    try {
      String url = MyOSSUtil.uploadFile(file);
      if (url != null) {
        // 保存到附件表
        Attachments attachments = new Attachments();
        attachments.setFilePath(url);
        attachments.setFileName(file.getOriginalFilename());
        //保留两位小数 四舍五入 1.0 * file.getSize() / 1024 / 1024
        attachments.setFileSize(Double.parseDouble(new DecimalFormat("#.##").format(1.0 * file.getSize() / 1024 / 1024)));
        attachments.setFileType(file.getContentType().substring(file.getContentType().indexOf("/") + 1));
        attachments.setModules(models);
        attachments.setState("0");
        // 保存到附件和模块关联表
        int t = attachmentsMapper.insert(attachments);
        if (t > 0) {
          return attachments.getId();
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return b;
  }

  /**
   * 保存到附件和模块关联表
   *
   * @param fileId  附件id
   * @param models  模块
   * @param tableId 表id
   * @return boolean 是否成功
   */
  public static Boolean fileModels(Integer fileId, String models, Integer tableId) {
    boolean b = false;
    try {
      FilesAndModules filesAndModules = new FilesAndModules();
      filesAndModules.setFileId(fileId);
      filesAndModules.setModel(models);
      filesAndModules.setTableId(tableId);
      int t = filesAndModulesMapper.insert(filesAndModules);
      if (t > 0) {
        b = true;
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return b;
  }

  /**
   * 批量保存到附件和模块关联表
   *
   * @param fileIds 附件id数组
   * @param models  模块
   * @param tableId 表id
   * @return boolean 是否成功
   */
  public static boolean fileModels(List<Integer> fileIds, String models, Integer tableId) {
    boolean b = false;
    int t = 0;
    try {
      for (Integer fileId : fileIds) {
        FilesAndModules filesAndModules = new FilesAndModules();
        filesAndModules.setFileId(fileId);
        filesAndModules.setModel(models);
        filesAndModules.setTableId(tableId);
        t += filesAndModulesMapper.insert(filesAndModules);
      }
      if (t == fileIds.size()) {
        b = true;
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return b;
  }

  /**
   * 批量删除附件和模块关联表
   *
   * @param fileIds 附件id数组
   * @return boolean 是否成功
   */
  public static boolean deleteFile(List<Integer> fileIds) {
    try {
      LambdaQueryWrapper<Attachments> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.in(Attachments::getId, fileIds);
      List<Attachments> attachments = attachmentsMapper.selectList(queryWrapper);
      for (Attachments attachment : attachments) {
        MyOSSUtil.deleteFile(attachment.getFilePath());
      }
      int s = attachmentsMapper.deleteBatchIds(fileIds);
      LambdaQueryWrapper<FilesAndModules> queryWrapper1 = new LambdaQueryWrapper<>();
      queryWrapper1.in(FilesAndModules::getFileId, fileIds);
      int t = filesAndModulesMapper.delete(queryWrapper1);
      return s > 0;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return false;
  }

  /**
   * 删除文件和模块关联表
   *
   * @param id    文件id
   * @param model 模块
   * @return boolean 是否成功
   */
  public static boolean deleteFileAndModel(Integer id, String model) {
    //删除文件
    LambdaQueryWrapper<FilesAndModules> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(FilesAndModules::getTableId, id);
    queryWrapper.eq(FilesAndModules::getModel, model);
    //查询文件和模块关联表 是否有数据
    long f = filesAndModulesMapper.selectCount(queryWrapper);
    if (f > 0) {
      int r = filesAndModulesMapper.delete(queryWrapper);
      return r > 0;
    }
    return true;
  }


}
